import React, {Component} from 'react';
import Logo from '../../components/logo/logo'
import {Button, InputItem, List, NavBar, WhiteSpace, WingBlank} from "antd-mobile";
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
import {faLock, faUser} from "@fortawesome/fontawesome-free-solid";

import {connect} from 'react-redux'
import {Redirect} from 'react-router-dom'
import {login} from '../../redux/actions'


class Login extends Component {
    state = {
        username: "",
        password: '',
    }

    handleChaneg(name, val) {
        this.setState({
            [name]: val
        });
    }

    login = () => {
        this.props.login(this.state)
    }
    resgister = () => {
        this.props.history.replace('/Register')
    }

    render() {
        let {redirectTo, msg} = this.props
        if (redirectTo) {
            console.log('redirectTo');
            return <Redirect to={redirectTo}/>
        }
        return (
            <div>
                <NavBar>navBar</NavBar>
                <Logo></Logo>
                <WingBlank>
                    {msg ? <p className='error-msg'>{msg}</p> : null}
                    <List className='register_list'>
                        <InputItem placeholder="用户名" onChange={(val) => {
                            this.handleChaneg('username', val)
                        }}>
                            <FontAwesomeIcon tabIndex='20' color='#999' icon={faUser}/>
                        </InputItem>
                        <WhiteSpace/>
                        <InputItem type='password' placeholder="密码" onChange={(val) => {
                            this.handleChaneg('password', val)
                        }}>
                            <FontAwesomeIcon color='#999' icon={faLock}/>
                        </InputItem>
                        <WhiteSpace/>
                        <Button onClick={this.login} type='primary'>登录</Button>
                        <WhiteSpace/>
                        <Button onClick={this.resgister}>注册</Button>
                    </List>

                </WingBlank>
            </div>
        );
    }
}

export default connect(
    state => state.user,
    {login}
)(Login);